<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 3.1.&nbsp; PHP and HTML Text</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-3.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-3-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-3-SECT-1"></a>
<h3 id="title-IDA53F1H" class="docSection1Title">3.1. PHP and HTML Text</h3>
<a name="IDX-CHP-3-0050"></a> 

<p class="docText">It's simple to output<a name="IDX-CHP-3-0051"></a> 
<a name="IDX-CHP-3-0052"></a> 
 text using PHP; in fact, handling text is one of PHP's specialties. We'll begin with detailing where PHP is processed, some of the basic functions to output text, and from there go right into printing text based on a certain condition being true.</p>
<a name="learnphpmysql-CHP-3-SECT-1.1"></a>
<h4 id="title-IDAY4F1H" class="docSection2Title">3.1.1. Text Output</h4>
<p class="docText">You'll want to be able to spit out text easily and often. PHP will let you do that, though you'll need to use proper PHP syntax when creating the code. Otherwise, your browser assumes that everything is HTML and outputs the PHP code directly to the browser, and then everything looks like text and code mixed up. This will certainly confuse your users! You can use whatever text editor you like to write your PHP code, including Notepad or DevPHP (<a class="docLink" target="_blank" href="http://sourceforge.net/projects/devphp/">http://sourceforge.net/projects/devphp/</a>).</p>
<p class="docText">Our examples will demonstrate how similar HTML markup and PHP code look and what you can do to start noticing the differences between them.</p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-3-EX-1">Example 3-1</a> is a simple HTML file that we'll use for an example.</p>
<a name="learnphpmysql-CHP-3-EX-1"></a><h5 id="title-IDAO5F1H" class="docExampleTitle">Example 3-1. All you need to start with PHP is a simple HTML document</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Hello World&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;p&gt;I sure wish I had something to say.&lt;/p&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre><br>

</td></TR></table></P>
<p class="docText">Nothing is special here; just your plain vanilla HTML file. However, you can enter PHP right into this file; for example, let's use PHP's <tt>echo</tt> command to output some text in <a class="docLink" href="#learnphpmysql-CHP-3-EX-2">Example 3-2</a>.</p>
<a name="learnphpmysql-CHP-3-EX-2"></a><H5 id="title-IDAEAG1H" class="docExampleTitle">Example 3-2. Adding some PHP code to the HTML file</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Hello World&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <span class="docEmphStrong">echo("&lt;p&gt;Now I have something to say.&lt;/p&gt;");</span>
    &lt;/body&gt;
&lt;/html&gt;
</pre><br>

</td></TR></table></p>
<a name="learnphpmysql-CHP-3-SECT-1.1.1"></a>
<h5 id="title-IDASAG1H" class="docSection3Title">3.1.1.1. Separating PHP from HTML</H5>
<a name="IDX-CHP-3-0053"></a> 
<a name="IDX-CHP-3-0054"></a> 

<p class="docText">This looks pretty simple, but there are some problems. There's no way to tell in this file which part is standard HTML and which part is PHP and therefore must be handled differently. To fix this, surround your PHP code in <tt>&lt;?php</tt> and <tt>?&gt;</tt> tags.</p>
<p class="docText">When you start writing PHP code, you'll be working with plain vanilla text files that contain PHP and HTML code. HTML is a simple markup language that designates how your page looks in a browser, but it is simply that: <span class="docEmphasis">text only</span>. The server doesn't have to process HTML files before sending them to the user's browser. Unlike HTML code, PHP code must be interpreted before the resulting page is sent to the browser. Otherwise, it will be one big mess on the user's screen.</p>
<p class="docText">To set apart the PHP code to inform the web server what needs to be processed, the PHP code is placed between formal or informal tags mixed with HTML. <a class="docLink" href="#learnphpmysql-CHP-3-EX-3">Example 3-3</a> uses <tt>echo</tt> and <tt>print</tt> operators to achieve this.</p>
<a name="learnphpmysql-CHP-3-EX-3"></a><h5 id="title-IDA2BG1H" class="docExampleTitle">Example 3-3. Calling echo and print</H5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Hello World&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <span class="docEmphStrong">&lt;?php</span>

        <span class="docEmphStrong">echo ("Hello world!&lt;br /&gt;");</span>
        <span class="docEmphStrong">print ('Goodbye.&lt;br /&gt;');</span>
        <span class="docEmphStrong">print 'Over and out.';</span>

        <span class="docEmphStrong">?&gt;</span>
    &lt;/body&gt;
&lt;/html&gt;
</pre><br>

</td></TR></table></p>
<p class="docText">When a browser requests this file, PHP<a name="IDX-CHP-3-0055"></a> 
 interprets it and produces HTML markup. <a class="docLink" href="#learnphpmysql-CHP-3-EX-4">Example 3-4</a> is the HTML that is produced from the code in <a class="docLink" href="#learnphpmysql-CHP-3-EX-3">Example 3-3</a>.</P>
<a name="learnphpmysql-CHP-3-EX-4"></a><h5 id="title-IDAMDG1H" class="docExampleTitle">Example 3-4. The HTML markup produced by the PHP code</H5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;html&gt;
&lt;head&gt;
        &lt;title&gt;Hello World&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        Hello world!&lt;br /&gt;Goodbye.&lt;br /&gt;Over and out.
    &lt;/body&gt;
&lt;/html&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">Save your HTML document to <span class="docEmphasis">C:\Program Files\Apache Group\Apache2\htdocs</span>, as we discussed in <a class="docLink" href="learnphpmysql-CHP-2.html#learnphpmysql-CHP-2">Chapter 2</a>. Open the file in a web browser, and you see something like <a class="docLink" href="#learnphpmysql-CHP-3-FIG-1">Figure 3-1</a>. The code in <a class="docLink" href="#learnphpmysql-CHP-3-EX-4">Example 3-4</a> is the same code that you see if you select View <img src=images/U2192.jpg border=0> Page Source from your browser's menu.</p>
<a name="learnphpmysql-CHP-3-FIG-1"></a><p><center>
<h5 class="docFigureTitle">Figure 3-1. The output as it appears in the web browser</h5>
<img border="0" alt="" width="549" height="149" SRC="images/learnphpmysql_0301.jpg">
</center></p><br>
<p class="docText">While writing PHP code, it's crucial to add comments<a name="IDX-CHP-3-0056"></a> 
 so that your code is easier to read and support. Most people don't remember exactly what they were thinking when they look at the code a year or more later, so let comments permeate your code and you'll be a happier PHPer in the future. PHP supports two styles of comments. We suggest using single-line comments for quick notes about a tricky part while using multiple-line comments when you need to describe something in greater depth; both are shown in <a class="docLink" href="#learnphpmysql-CHP-3-EX-5">Example 3-5</a>.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><TD valign="top">
<p class="docText">Comments are retained in the PHP file, but the interpreter doesn't output the PHP comments. The interpreter outputs only the HTML comments.</p>
</TD></tr></table></td></tr></table></P><br>
<a name="learnphpmysql-CHP-3-EX-5"></a><h5 id="title-IDAKFG1H" class="docExampleTitle">Example 3-5. Using comments to make your code easier to read</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Hello World&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;?php

        // A single line comment could say that we are going to
        // print hello world.

        /* This is how to do a
           multi-line comment and could be used comment out a block
           of code */

        echo("Hello world!&lt;br /&gt;");
        print('Goodbye.&lt;br /&gt;');

        ?&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-3-EX-5">Example 3-5</a>, two comment styles are used: <tt>//</tt> for single-line comments and <tt>/*</tt> ... <tt>*/</tt> for multiline comments. Keep in mind that if you want to place a comment in HTML markup, you need to use the open comment <tt>&lt;!</tt> and close comment <tt>&gt;</tt> tags.</P>
<p class="docText">A semicolon (<tt>;</tt>) ends all code statements in PHP. (Because of this, semicolons can't be used in names.) It's good style as well as practical to also start a new line after your semicolon so the code is easier to read.</P>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><TD width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><TD valign="top">
<p class="docText">Since PHP files tend to switch back and forth between PHP code and HTML markup, using an HTML comment in the middle of PHP or a PHP comment in the middle of HTML makes a mess of your page, so be extra vigilant in not doing this!</p>
</TD></TR></table></TD></tr></table></p><br>
<p class="docText">The PHP files get to your web site just like any other file. To try the PHP code in <a class="docLink" href="learnphpmysql-CHP-3-SECT-2.html#learnphpmysql-CHP-3-EX-6">Example 3-6</a>, save the file in the document root that you selected when you installed Apache in <a class="docLink" href="learnphpmysql-CHP-2.html#learnphpmysql-CHP-2">Chapter 2</a>. Once you have your PHP filesay, <span class="docEmphasis">example.php</span>in your web accessible directory, you can view it by browsing to <a class="docLink" target="_blank" href="http://yourdomain.com/your_directory/example.php">http://yourdomain.com/your_directory/example.php</a>.</p>
<p class="docText">Now that you know how to include PHP code within your HTML markup and not let your user see a bunch of gobbledygook, we'll explore basic PHP programming.</p>



<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-3.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-3-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
